<!DOCTYPE html>
<html>
	<head>
		<title>角色授权设置</title>
		<!-- css -->
		<link href="../css/themes/base/easyui.css" rel="stylesheet" />
		<link href="../css/themes/skin-coolblack.css" rel="stylesheet" />
		<link href="../css/site.css" rel="stylesheet" />
		<link href="../css/fontawesome/css/font-awesome.min.css" rel="stylesheet" />
		<style>
			.over{top:0;left:0;z-index:9999999;width:100%;height:100%;background-color:#f5f5f5;opacity:.5}
			.layout,.over{position:absolute;display:none}
			.layout{top:40%;left:40%;z-index:100000000;width:20%;height:20%;text-align:center}
		</style>

		<script src="../js/utils/jquery.min.js" type="text/javascript"></script>
		<script src="../js/utils/baseUrl.js"></script>
		<script src="../js/easyui/jquery.easyui.min.js" type="text/javascript"></script>
		<script src="../js/easyui/easyui-lang-zh-CN.js" type="text/javascript"></script>
		<script src="../js/easyui/jquery.easyui.plus.js"></script>
		<script src="../js/utils/common.js"></script>
		<script src="../js/utils/jquery.form.js"></script>

	</head>
	<body>
		<div style="padding:4px 5px 0px 5px;">


			<div class="mvctool">

				菜单类型切换： <select class="easyui-combobox" name="swicthType" id="swicthType" style="width:80px;height:26px;margin-right:10px; ">
					<option value="menu">系统菜单</option>
					<option value="api">API接口</option>
				</select>

				<a id="btnSave" class="btn btn-success"><span class="fa fa-save"></span>&nbsp;保存操作和字段权限</a>
			</div>

			<table style="width: 100%">
				<tbody>
					<tr>
						<td style="vertical-align: top">
							<!--获取角色组-->
							<table id="roleList"></table>
						</td>
						<td style="width:3px;"></td>
						<td style="width: 300px; vertical-align: top">
							<!--获取模块-->
							<table id="moduleList"></table>
						</td>
						<td style="width:3px;"></td>
						<td id="right1" style="vertical-align: top;width:200px;">
							<!--获取操作码-->
							<table id="operateList"></table>
						</td>
						<td style="width:3px;"></td>
						<td id="right2" style="vertical-align: top;width:200px;">
							<!--获取字段权限-->
							<table id="dataList"></table>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
		<div id="over" class="over"></div>
		<div id="layout" class="layout"><img src="../img/loading.gif" /></div>
		<div id="fullSetContent"></div>
		<script type="text/javascript">
			$('#swicthType').combobox({
				editable: false,
				onSelect: function(record) {
					if (record.value == "api") {
						$('#moduleList').treegrid({
							"url": baseUrl + '/zh-CN/SysRight/GetModelList?id=ApiInterfaceAuth'
						});

					} else {
						$('#moduleList').treegrid({
							"url": baseUrl + '/zh-CN/SysRight/GetModelList?id=0'
						});

					}

				}
			});

			$(function() {
				var curModuleId, curRoleId, curModuleName, curRoleName, curSystemId, curSystemName; //选择的模块ID,选中的角色ID，选中的模块名称，角色名称
				curRoleName = "?";
				curModuleName = "?";
				$('#roleList').datagrid({
					url: baseUrl + '/zh-CN/SysRight/GetRoleList',
					width: SetGridWidthSub(915),
					methord: 'post',
					height: SetGridHeightSub(45),
					fitColumns: true,
					sortName: 'CreateTime',
					sortOrder: 'desc',
					idField: 'Id',
					pageSize: 15,
					pageList: [15, 20, 30, 40, 50],
					showPageList: false,
					showRefresh: false,
					displayMsg: '',
					pagination: true,
					striped: true, //奇偶行是否区分
					singleSelect: true, //单选模式
					rownumbers: true, //行号
					title: '角色列表',
					onLoadSuccess: function(data) {
						$('#operateList').datagrid('loadData', {
							total: 0,
							rows: []
						});
					},
					columns: [
						[{
								field: 'Id',
								title: '',
								width: 80,
								hidden: true
							},
							{
								field: 'Name',
								title: '角色组',
								width: 80,
								sortable: true
							},
							{
								field: 'Description',
								title: '说明',
								width: 80,
								sortable: true
							},
							{
								field: 'CreateTime',
								title: '创建时间',
								width: 80,
								sortable: true
							},
							{
								field: 'CreatePerson',
								title: '',
								width: 80,
								sortable: true,
								hidden: true
							}
						]
					],
					onClickRow: function(index, data) {
						var row = $('#roleList').datagrid('getSelected');
						if (row != null) {
							curRoleName = row.Name;
							curRoleId = row.Id;
							$('#operateList').datagrid({
								url: baseUrl + "/SysRight/GetRightByRoleAndModule?roleId=" + curRoleId + "&moduleId=" + curModuleId +
									""
							});
							$('#operateList').datagrid({
								'title': "角色组: " + curRoleName + " >> 模块：" + curModuleName
							});

							$('#dataList').datagrid({
								url: baseUrl + "/SysRight/GetRightDataByRoleAndModule?roleId=" + curRoleId + "&moduleId=" + curModuleId +
									""
							});
							$('#dataList').datagrid({
								'title': "角色组: " + curRoleName + " >> 模块：" + curModuleName
							});
						}
					}
				}).datagrid('getPager').pagination({
					showPageList: false,
					showRefresh: false
				});
				$('#moduleList').treegrid({
					url: baseUrl + '/zh-CN/SysRight/GetModelList',
					width: 300,
					methord: 'post',
					height: SetGridHeightSub(45),
					fitColumns: true,
					treeField: 'Name',
					idField: 'Id',
					pagination: false,
					striped: true, //奇偶行是否区分
					singleSelect: true, //单选模式
					title: '模块列表',
					columns: [
						[{
								field: 'Id',
								title: '唯一标识',
								width: 120,
								hidden: true
							},
							{
								field: 'Name',
								title: '名称',
								width: 220,
								sortable: false
							},
							{
								field: 'EnglishName',
								title: '英文名称',
								width: 80,
								sortable: false,
								hidden: true
							},
							{
								field: 'ParentId',
								title: '上级Id',
								width: 80,
								sortable: false,
								hidden: true
							},
							{
								field: 'Url',
								title: '链接地址',
								width: 80,
								sortable: false,
								hidden: true
							},
							{
								field: 'Iconic',
								title: '图标',
								width: 80,
								sortable: false,
								hidden: true
							},
							{
								field: 'Sort',
								title: '排序号',
								width: 80,
								sortable: false,
								hidden: true
							},
							{
								field: 'Remark',
								title: '说明',
								width: 80,
								sortable: false,
								hidden: true
							},
							{
								field: 'Enable',
								title: '启用',
								width: 60,
								align: 'center',
								formatter: function(value) {
									if (value) {
										return "<span class='color-green fa fa-circle'></span>";
									} else {
										return "<span class='color-blue fa fa-circle'></span>";
									}
								},
								hidden: true
							},
							{
								field: 'CreatePerson',
								title: '创建人',
								width: 80,
								sortable: true,
								hidden: true
							},
							{
								field: 'CreateTime',
								title: '创建时间',
								width: 120,
								sortable: true,
								hidden: true
							},
							{
								field: 'IsLast',
								title: '是否最后一项',
								align: 'center',
								width: 100,
								formatter: function(value) {
									if (value) {
										return "是";
									} else {
										return "否";
									}
								},
								hidden: true
							},
						]
					],
					onClickRow: function(index, data) {
						var row = $('#moduleList').treegrid('getSelected');
						if (row != null) {
							curModuleName = row.Name;
							curModuleId = row.Id;
							if (curRoleId == null && row.IsLast) {
								$.messageBox5s('提示', "请再选择一个角色！");
								return;
							}
							$('#operateList').datagrid({
								url: baseUrl + "/SysRight/GetRightByRoleAndModule?roleId=" + curRoleId + "&moduleId=" + curModuleId +
									""
							});
							$('#operateList').datagrid({
								'title': "角色组: " + curRoleName + " >> 模块：" + (row.IsLast ? curModuleName : "[请再选择最后菜单项]")
							});

							$('#dataList').datagrid({
								url: baseUrl + "/SysRight/GetRightDataByRoleAndModule?roleId=" + curRoleId + "&moduleId=" + curModuleId +
									""
							});
							$('#dataList').datagrid({
								'title': "角色组: " + curRoleName + " >> 模块：" + (row.IsLast ? curModuleName : "[请再选择最后菜单项]")
							});

						}

					}
				});
				$('#operateList').datagrid({
					url: baseUrl + '/zh-CN/SysRight/GetRightByRoleAndModule',
					width: 300,
					methord: 'post',
					fitColumns: true,
					sortName: 'CreateTime',
					sortOrder: 'desc',
					idField: 'Id',
					striped: true, //奇偶行是否区分
					singleSelect: true, //单选模式
					title: '授权操作',
					//rownumbers: true,//行号
					columns: [
						[{
								field: 'Ids',
								title: 'Ids',
								width: 70,
								hidden: true
							},
							{
								field: 'Name',
								title: '名称',
								width: 70,
								sortable: true
							},
							{
								field: 'KeyCode',
								title: '操作码',
								width: 60,
								sortable: true
							},
							{
								field: 'IsValid',
								title: "<a href='#' title='选择' class=\"fa  fa-check-square-o color-green\"  onclick=\"SelAll1();\"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='#' title='反选' class=\"fa  fa-square-o color-gray\" onclick=\"UnSelAll1();\"  ></a>",
								align: 'center',
								width: 60,
								formatter: function(value) {
									if (value) {
										return "<input type='checkbox' checked='checked' value=" + value + " />";
									} else {
										return "<input type='checkbox' value=" + value + " />";
									}
								},
							},
							{
								field: 'RightId',
								title: '模块ID',
								width: 80,
								sortable: true,
								hidden: true
							}
						]
					]
				});
				$('#dataList').datagrid({
					url: baseUrl + '/zh-CN/SysRight/GetRightDataByRoleAndModule',
					width: 300,
					methord: 'post',
					fitColumns: true,
					sortName: 'CreateTime',
					sortOrder: 'desc',
					idField: 'Id',
					striped: true, //奇偶行是否区分
					singleSelect: true, //单选模式
					title: '字段禁用授权操作',
					//rownumbers: true,//行号
					columns: [
						[{
								field: 'Ids',
								title: 'Ids',
								width: 70,
								hidden: true
							},
							{
								field: 'Name',
								title: '字段描述',
								width: 70,
								sortable: true
							},
							{
								field: 'KeyCode',
								title: '字段',
								width: 60,
								sortable: true
							},
							{
								field: 'IsValid',
								title: "<a href='#' title='选择' class=\"fa  fa-check-square-o color-green\"  onclick=\"SelAll2();\"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='#' title='反选' class=\"fa  fa-square-o color-gray\" onclick=\"UnSelAll2();\"  ></a>",
								align: 'center',
								width: 60,
								formatter: function(value) {
									if (value) {
										return "<input type='checkbox' checked='checked' value=" + value + " />";
									} else {
										return "<input type='checkbox' value=" + value + " />";
									}
								},
							},
							{
								field: 'RightId',
								title: '模块ID',
								width: 80,
								sortable: true,
								hidden: true
							}
						]
					]
				});
				$("#btnSave").click(function() {
					var updateRows = 0;
					var rows = $("#operateList").datagrid("getRows"); //这段代码是获取当前页的所有行。

					var updateDataRows = 0;
					var dataRows = $("#dataList").datagrid("getRows");

					//处理操作码
					for (var i = 0; i < rows.length; i++) {
						var setFlag = $("#right1 td[field='IsValid'] input").eq(i).prop("checked");
						var ids = rows[i].Ids;
						var rightId = rows[i].RightId;
						var keyCode = rows[i].KeyCode;
						var isValid = rows[i].IsValid;
						if (isValid != setFlag) //判断是否有作修改
						{
							$.ajax({
								url: baseUrl + '/zh-CN/SysRight/UpdateRight',
								async: false, // 注意此处需要同步，因为返回完数据后，下面才能让结果的第一条selected
								type: "POST",
								data: {
									"Id": ids,
									"RightId": rightId,
									"KeyCode": keyCode,
									"IsValid": setFlag
								},
								dataType: "json",
								success: function(fields) {

								}
							});
							updateRows++;
						}
					}
					//处理字段
					for (var i = 0; i < dataRows.length; i++) {
						var setFlag = $("#right2 td[field='IsValid'] input").eq(i).prop("checked");
						var ids = dataRows[i].Ids;
						var rightId = dataRows[i].RightId;
						var keyCode = dataRows[i].KeyCode;
						var isValid = dataRows[i].IsValid;
						if (isValid != setFlag) //判断是否有作修改
						{
							$.ajax({
								url: baseUrl + '/zh-CN/SysRight/UpdateDataRight',
								async: false, // 注意此处需要同步，因为返回完数据后，下面才能让结果的第一条selected
								type: "POST",
								data: {
									"Id": ids,
									"RightId": rightId,
									"KeyCode": keyCode,
									"IsValid": setFlag
								},
								dataType: "json",
								success: function(fields) {

								}
							});
							updateRows++;
						}
					}

					if (updateRows > 0 || updateDataRows > 0) {
						$.messageBox5s('提示', '保存成功！');
						if (updateRows > 0) {
							$("#operateList").datagrid("reload");
						}
						if (updateDataRows > 0) {
							$("#dataList").datagrid("reload");
						}
					} else {
						$.messageBox5s('提示', '没有任何数据改变！');
					}

				});
				$(window).resize(function() {
					resizeLayout();
				});
				resizeLayout();
			});

			function resizeLayout() {
				setTimeout(function() {
					$('#operateList,#dataList').datagrid('resize', {
						height: SetGridHeightSub(45)
					});
					$('#roleList').datagrid('resize', {
						height: SetGridHeightSub(45),
						width: SetGridWidthSub(915),
					});
				}, 100);
			}

			function SelAll1() {
				$("#right1 td[field='IsValid'] input").prop("checked", true);
				$("#btnSave").trigger("click");
				return;
			}

			function UnSelAll1() {
				$("#right1 td[field='IsValid'] input").prop("checked", false);
				$("#btnSave").trigger("click");
				return;
			}

			function SelAll2() {
				$("#right2 td[field='IsValid'] input").prop("checked", true);
				$("#btnSave").trigger("click");
				return;
			}

			function UnSelAll2() {
				$("#right2 td[field='IsValid'] input").prop("checked", false);
				$("#btnSave").trigger("click");
				return;
			}

			$(function() {
				if (isExitsFunction(window.parent.fullSetButtonOut)) {
					$("#fullSetContent").html(window.parent.fullSetButtonOut());
					$("#fullSetButton").click(function() {
						if ($(this).attr("class") == "fa fa-expand") {
							$(this).removeClass("fa-expand").addClass("fa-compress");
						} else {
							$(this).removeClass("fa-compress").addClass("fa-expand");
						}
						window.parent.fullSet();
					});
				}
			});
		</script>
	</body>
</html>
